using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;

public partial class admin_menu : System.Web.UI.UserControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsCallback)
			BindMenu();
	}

	private void BindMenu()
	{
		if (SiteMap.Providers["AdminSiteMap"] == null || SiteMap.Providers["AdminSiteMap"].RootNode == null)
			return;

		foreach (SiteMapNode node in SiteMap.Providers["AdminSiteMap"].RootNode.ChildNodes)
		{
			if (node.IsAccessibleToUser(Context))
			{
				bool current = Request.RawUrl.Contains(ResolveUrl(node.Url.Replace("#top", String.Empty)));

				// add to menu
				menu.Controls.Add(GetItem(node, current));

				if (current && node.HasChildNodes)
				{
					subMenu.Visible = true;
					foreach (SiteMapNode subNode in node.ChildNodes)
					{
						bool subCurrent = Request.RawUrl.Contains(ResolveUrl(subNode.Url));

						// add to sub-menu
						subMenu.Controls.Add(GetItem(subNode, subCurrent));
					}
				}
			}
		}
	}

	private HtmlGenericControl GetItem(SiteMapNode node, bool current)
	{
		string url = node.Url.Replace("#top", String.Empty);
		string text = node.Title;

		// create link
		HtmlAnchor a = new HtmlAnchor();
		a.HRef = url;
		a.InnerHtml = HttpUtility.HtmlEncode(text);

		// create list item
		HtmlGenericControl li = new HtmlGenericControl("li");
		li.Controls.Add(a);
		li.Attributes["class"] = current ? "tab current" : "tab";

		return li;
	}
}